home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.01 Jan 93 / Getting Started / WindowMaster.c next >
Encoding:
C/C++ Source or Header  |  1992-10-20  |  5.4 KB  |  279 lines  |  [TEXT/KAHL]

  1. #include <Values.h>
  2.  
  3. #define kBaseResID            128
  4. #define kMoveToFront            (WindowPtr)-1L
  5. #define kSleep                    MAXLONG
  6.  
  7. #define kScrollBarAdjust    (16-1)
  8. #define kLeaveWhereItIs        false
  9. #define kNormalUpdates        true
  10.  
  11. #define kMinWindowHeight    50
  12. #define kMinWindowWidth        80
  13.  
  14.  
  15. /*************/
  16. /*  Globals  */
  17. /*************/
  18.  
  19. Boolean        gDone;
  20.  
  21.  
  22. /***************/
  23. /*  Functions  */
  24. /***************/
  25.  
  26. void    ToolBoxInit( void );
  27. void    WindowInit( void );
  28. void    EventLoop( void );
  29. void    DoEvent( EventRecord *eventPtr );
  30. void    HandleMouseDown( EventRecord *eventPtr );
  31. void    DoUpdate( EventRecord *eventPtr );
  32. void    DoPicture( WindowPtr window, PicHandle picture );
  33. void    DoActivate( WindowPtr window, Boolean becomingActive );
  34. void    DoSuspendResume( Boolean resuming );
  35. void    CenterPict( PicHandle picture, Rect *srcRectPtr,
  36.                     Rect *destRectPtr );
  37.  
  38.  
  39. /******************************** main *********/
  40.  
  41. void    main( void )
  42. {
  43.     ToolBoxInit();
  44.     WindowInit();
  45.     
  46.     EventLoop();
  47. }
  48.  
  49.  
  50. /*********************************** ToolBoxInit */
  51.  
  52. void    ToolBoxInit( void )
  53. {
  54.     InitGraf( &thePort );
  55.     InitFonts();
  56.     InitWindows();
  57.     InitMenus();
  58.     TEInit();
  59.     InitDialogs( nil );
  60.     InitCursor();
  61. }
  62.  
  63.  
  64. /******************************** WindowInit *********/
  65.  
  66. void    WindowInit( void )
  67. {
  68.     WindowPtr    window;
  69.     
  70.     window = GetNewWindow( kBaseResID, nil, kMoveToFront );
  71.     
  72.     if ( window == nil )
  73.     {
  74.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  75.         ExitToShell();
  76.     }
  77.     
  78.     ShowWindow( window );
  79.     SetPort( window );
  80. }
  81.  
  82.  
  83. /******************************** EventLoop *********/
  84.  
  85. void    EventLoop( void )
  86. {        
  87.     EventRecord        event;
  88.     
  89.     gDone = false;
  90.     while ( gDone == false )
  91.     {
  92.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  93.             DoEvent( &event );
  94.     }
  95. }
  96.  
  97.  
  98. /************************************* DoEvent     */
  99.  
  100. void    DoEvent( EventRecord *eventPtr )
  101. {
  102.     Boolean    becomingActive, resuming;
  103.     
  104.     switch ( eventPtr->what )
  105.     {
  106.         case mouseDown: 
  107.             HandleMouseDown( eventPtr );
  108.             break;
  109.         case updateEvt:
  110.             DoUpdate( eventPtr );
  111.             break;
  112.         case activateEvt:
  113.             becomingActive = ( (eventPtr->modifiers & activeFlag)
  114.                                         == activeFlag );
  115.             DoActivate( (WindowPtr)eventPtr->message, becomingActive );
  116.             break;
  117.         case osEvt:
  118.             resuming = ( eventPtr->message & suspendResumeMessage )
  119.                                         == resumeFlag;
  120.             DoSuspendResume( resuming );
  121.             break;
  122.     }
  123. }
  124.  
  125.  
  126. /************************************* HandleMouseDown */
  127.  
  128. void    HandleMouseDown( EventRecord *eventPtr )
  129. {
  130.     WindowPtr    window;
  131.     short            thePart;
  132.     GrafPtr        oldPort;
  133.     long            windSize;
  134.     Rect            growRect;
  135.     
  136.     thePart = FindWindow( eventPtr->where, &window );
  137.     
  138.     switch ( thePart )
  139.     {
  140.         case inSysWindow : 
  141.             SystemClick( eventPtr, window );
  142.             break;
  143.         case inContent:
  144.             SelectWindow( window );
  145.             break;
  146.         case inDrag : 
  147.             DragWindow( window, eventPtr->where, &screenBits.bounds );
  148.             break;
  149.         case inGoAway :
  150.             if ( TrackGoAway( window, eventPtr->where ) )
  151.                 gDone = true;
  152.             break;
  153.         case inGrow:
  154.             growRect.top = kMinWindowHeight;
  155.             growRect.left = kMinWindowWidth;
  156.             growRect.bottom = MAXSHORT;
  157.             growRect.right = MAXSHORT;
  158.             
  159.             windSize = GrowWindow( window, eventPtr->where, &growRect );
  160.             if ( windSize != 0 )
  161.             {
  162.                 GetPort( &oldPort );
  163.                 SetPort( window );
  164.                 EraseRect( &window->portRect );
  165.                 SizeWindow( window, LoWord( windSize ),
  166.                         HiWord( windSize ), kNormalUpdates );
  167.                 InvalRect( &window->portRect );
  168.                 SetPort( oldPort );
  169.             }
  170.             break;
  171.         case inZoomIn:
  172.         case inZoomOut:
  173.             if ( TrackBox( window, eventPtr->where, thePart ) )
  174.             {
  175.                 GetPort( &oldPort );
  176.                 SetPort( window );
  177.                 EraseRect( &window->portRect );
  178.                 ZoomWindow( window, thePart, kLeaveWhereItIs );
  179.                 InvalRect( &window->portRect );
  180.                 SetPort( oldPort );
  181.             }
  182.             break;
  183.     }
  184. }
  185.  
  186.  
  187. /************************************* DoUpdate     */
  188.  
  189. void    DoUpdate( EventRecord *eventPtr )
  190. {
  191.     short            pictureID;
  192.     PicHandle    picture;
  193.     WindowPtr    window;
  194.     
  195.     window = (WindowPtr)eventPtr->message;
  196.     
  197.     BeginUpdate( window );
  198.     
  199.     picture = GetPicture( kBaseResID );
  200.     
  201.     if ( picture == nil )
  202.     {
  203.         SysBeep( 10 );    /*  Couldn't load the PICT resource!!!  */
  204.         ExitToShell();
  205.     }
  206.     
  207.     DoPicture( window, picture );
  208.     
  209.     EndUpdate( window );
  210. }
  211.  
  212.  
  213. /******************************** DoPicture *********/
  214.  
  215. void    DoPicture( WindowPtr window, PicHandle picture )
  216. {
  217.     Rect            drawingClipRect, destRect;
  218.     RgnHandle    tempRgn;
  219.     
  220.     SetPort( window );
  221.     
  222.     EraseRect( &window->portRect );
  223.     
  224.     tempRgn = NewRgn();
  225.     GetClip( tempRgn );
  226.  
  227.     drawingClipRect = window->portRect;
  228.     drawingClipRect.right -= kScrollBarAdjust;
  229.     drawingClipRect.bottom -= kScrollBarAdjust;
  230.     
  231.     ClipRect( &drawingClipRect );
  232.     
  233.     CenterPict( picture, &drawingClipRect, &destRect );
  234.     DrawPicture( picture, &destRect );
  235.     
  236.     SetClip( tempRgn );
  237.     DisposeRgn( tempRgn );
  238.     
  239.     DrawGrowIcon( window );
  240. }
  241.  
  242.  
  243. /************************************* DoActivate     */
  244.  
  245. void    DoActivate( WindowPtr window, Boolean becomingActive )
  246. {
  247.     DrawGrowIcon( window );
  248. }
  249.  
  250.  
  251. /************************************* DoSuspendResume     */
  252.  
  253. void    DoSuspendResume( Boolean resuming )
  254. {
  255.     WindowPtr    window;
  256.     
  257.     window = FrontWindow();
  258.     
  259.     DrawGrowIcon( window );
  260. }
  261.  
  262.  
  263. /****************** CenterPict ********************/
  264.  
  265. void    CenterPict( PicHandle picture, Rect *srcRectPtr,
  266.                             Rect *destRectPtr )
  267. {
  268.     Rect    pictRect;
  269.     
  270.     pictRect = (**( picture )).picFrame;
  271.     
  272.     OffsetRect( &pictRect, srcRectPtr->left - pictRect.left,
  273.                            srcRectPtr->top     - pictRect.top);
  274.     OffsetRect( &pictRect,(srcRectPtr->right - pictRect.right)/2,
  275.                           (srcRectPtr->bottom - pictRect.bottom)/2);
  276.                           
  277.     *destRectPtr = pictRect;
  278. }
  279.